package cooperate

import (
	"gddgame.cc/galaxy/common/namespaces/cooperate/structure"
	"gddgame.cc/galaxy/utils/serialize"
)

type EventBuilder struct {
	execute *LogicExecute
	mapNoId string

	content serialize.IStruct
	mode    structure.EventMode
	event   byte
}

func (logic *LogicExecute) EventBuilder() *EventBuilder {
	return &EventBuilder{
		execute: logic,
	}
}

func (builder *EventBuilder) BindMapNoId(mapNoId string) *EventBuilder {
	builder.mapNoId = mapNoId
	return builder
}

func (builder *EventBuilder) Data() structure.EventData {
	b, _ := builder.content.MarshalMsg(nil)
	data := structure.EventData{
		Mode:    builder.mode,
		Content: b,
	}
	return data
}
func (builder *EventBuilder) Schedule(scheduleBuilder *ScheduleBuilder, check *CheckBuilder) *EventBuilder {
	content := scheduleBuilder.Content()
	if check != nil {
		content.Checks = check.Data()
	}
	builder.content = content
	builder.mode = ScheduleMode
	return builder
}

func (builder *EventBuilder) Operate(operateBuilder *OperateBuilder, check *CheckBuilder) *EventBuilder {
	content := operateBuilder.Content()
	if check != nil {
		content.Checks = check.Data()
	}
	builder.content = content
	builder.mode = OperateMode
	return builder
}

func (builder *EventBuilder) Task(taskBuilder *TaskBuilder, check *CheckBuilder) *EventBuilder {
	content := taskBuilder.Content()
	if check != nil {
		content.Checks = check.Data()
	}
	builder.content = content
	builder.mode = TaskMode
	return builder
}

func (logic *LogicExecute) MakeOperateEvent(operates []OperateData, checks [][]CheckData) structure.EventData {
	for index, _ := range operates {
		if operates[index].inline != nil {
			b, _ := operates[index].inline.MarshalMsg(nil)
			operates[index].Data = b
		}
	}
	e := structure.EventData{
		Mode:    OperateMode,
		Content: nil,
	}
	o := &OperateContent{
		Checks:   checks,
		Operates: operates,
		Message:  nil,
	}
	b, _ := o.MarshalMsg(nil)
	e.Content = b
	return e
}
func (logic *LogicExecute) MakeScheduleEvent(key string, time int, schedule structure.ScheduleLogic, content []byte) structure.EventData {
	e := structure.EventData{
		Mode:    ScheduleMode,
		Content: nil,
	}
	o := &ScheduleContent{
		Checks:   nil,
		Schedule: schedule,
		Key:      key,
		Time:     logic.FormatTime(int64(time)),
		Content:  content,
	}
	b, _ := o.MarshalMsg(nil)
	e.Content = b
	return e
}

func (logic *LogicExecute) MakeBindEvent(current []byte, eb []EventBody) structure.EventData {
	e := structure.EventData{
		Mode:    BindEventMode,
		Content: nil,
	}
	o := &EventContent{
		Checks:  nil,
		Current: current,
		Events:  eb,
	}
	b, _ := o.MarshalMsg(nil)
	e.Content = b
	return e
}

func (logic *LogicExecute) MakeQuery(accountId int, identity int, event byte, params []interface{}, checks [][]CheckData) structure.EventData {
	e := structure.EventData{
		Mode:    QueryMode,
		Content: nil,
	}
	o := &QueryContent{
		Checks:    checks,
		AccountId: accountId,
		Identity:  identity,
		Event:     event,
		Params:    params,
	}
	b, _ := o.MarshalMsg(nil)
	e.Content = b
	return e
}
